﻿using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.TeamFoundation.VersionControl.Client;
using NLog;
using TfsGiggle.Core.Parsers;

namespace TfsGiggle.Core.Tfs.Implementation
{
    /// <summary>
    /// Wrapper for a Tfs VersionControl item
    /// </summary>
    class ItemImpl : IItem
    {
        private static readonly LoggerWithEventId _logger = (LoggerWithEventId)LogManager.GetCurrentClassLogger(typeof(LoggerWithEventId));
        Item _item;
        IVersionControlServer _versionControlServer;
        IParser _parser;

        /// <summary>
        /// ctor
        /// </summary>
        /// <param name="versionControlServer"></param>
        /// <param name="item"></param>
        public ItemImpl(IVersionControlServer versionControlServer, Item item)
        {
            _item = item;
            _versionControlServer = versionControlServer;
        }

        public void Parse()
        {
            _parser = ParserFactoryManager.GetParser(_item.ServerItem);
            if (_parser != null)
            {
                try
                {
                    _logger.Trace("Parsing start for '{0}' using parser '{1}'", _item.ServerItem, _parser.Name);
                    _parser.Parse(_item.ServerItem, new StreamReader(DownloadFile()).ReadToEnd());
                    _logger.Trace("Parsing ended for '{0}' using parser '{1}'", _item.ServerItem, _parser.Name);
                }
                catch (Exception ex)
                {
                    _logger.WarnException("ItemImpl.Exception", ex);
                }
            }
        }

        /// <summary>
        /// Id
        /// </summary>
        public int ChangesetId
        {
            get { return _item.ChangesetId; }
        }


        /// <summary>
        /// Stream to download file
        /// </summary>
        /// <returns></returns>
        public Stream DownloadFile()
        {
            return _item.DownloadFile();
        }

        /// <summary>
        /// Encoding
        /// </summary>
        public int Encoding
        {
            get { return _item.Encoding; }
        }

        /// <summary>
        /// Content length
        /// </summary>
        public long ContentLength
        {
            get { return _item.ContentLength; }
        }

        /// <summary>
        /// VersionControl Path for item
        /// </summary>
        public string ServerItem
        {
            get { return _item.ServerItem; }
        }

        /// <summary>
        /// Checkin date
        /// </summary>
        public DateTime CheckinDate
        {
            get { return _item.CheckinDate; }
        }

        /// <summary>
        /// VersionControl server
        /// </summary>
        public IVersionControlServer VersionControlServer
        {
            get { return _versionControlServer; }
        }

        /// <summary>
        /// Usings
        /// </summary>
        public List<string> UsingDeclarations
        {
            get
            {
                return _parser == null ? null : _parser.UsingDeclarations;
            }
        }

        /// <summary>
        /// Classes
        /// </summary>
        public List<string> ClassDeclarations
        {
            get
            {
                return _parser == null ? null : _parser.ClassDeclarations;
            }
        }

        /// <summary>
        /// Interfaces
        /// </summary>
        public List<string> InterfaceDeclarations
        {
            get
            {
                return _parser == null ? null : _parser.InterfaceDeclarations;
            }
        }

        /// <summary>
        /// Methods
        /// </summary>
        public List<string> MethodDeclarations
        {
            get
            {
                return _parser == null ? null : _parser.MethodDeclarations;
            }
        }
    }
}